/*
 * @lc app=leetcode.cn id=383 lang=cpp
 *
 * [383] 赎金信
 */

#include <limits.h>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <string>
using namespace std;

// @lc code=start
class Solution {
public:
    bool canConstruct(string ransomNote, string magazine) {
        map<char, int> records;         // 记录每个字符的次数

        // 初始化 records
        for (char i = 'a'; i <= 'z'; i++) {
            records.insert(make_pair(i, 0));
        }
        
        // 遍历 magazine
        for (int i = 0; i < magazine.length(); i++) {
            records[magazine[i]]++;
        }

        // 遍历 ransomNote
        for (int i = 0; i < ransomNote.length(); i++) {
            if (records[ransomNote[i]] == 0) {
                return false;
            } else {
                records[ransomNote[i]]--;
            }
        }
        
        return true;
    }
};
// @lc code=end

